<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />

	<title>Heart Defender</title>

	<link rel="stylesheet" href="css/fonts.css" />
	<link rel="stylesheet" href="css/style.css" />

	<link rel="icon" type="image/png" href="img/jewel_ico.png" />

	<script src="js/functions.js"></script>
	<script src="js/Input.js"></script>
	<!--<script src="lib/headtrackr/headtrackr.js"></script>--><!-- DOCUMENTATION : https://github.com/auduno/headtrackr -->
	<script src="lib/ImageInGame/IIG.min.js"></script><!-- DOCUMENTATION : https://github.com/jmpp/ImageInGame.js -->
	<script src="js/MusicLoader.js"></script>

	<!-- Fichiers de configuration -->
	<script src="conf/Game.cfg.js"></script>
	<script src="conf/Projectile.cfg.js"></script>
	<script src="conf/Tache.cfg.js"></script>
	<script src="conf/Score.cfg.js"></script>

	<!-- Fichiers sources -->
	<script src="js/Player.js"></script>
	<script src="js/Enemy.js"></script>
	<script src="js/Boss.js"></script>
	<script src="js/Message.js"></script>
	<script src="js/Board.js"></script>
	<script src="js/Score.js"></script>
	<script src="js/Heart.js"></script>
	<script src="js/Bonus.js"></script>
	<script src="js/Projectile.js"></script>
	<script src="js/Tache.js"></script>

	<!-- Fichiers écrans -->
	<script src="js/Achievement.js"></script>
	
	<!-- Fichier core -->
	<script src="js/Game.js"></script>

	<!-- Tutorial -->
	<script src="js/Tuto.js"></script>

</head>
<body role="main">
	
	<div id="gameArea">
		<canvas id="gameCanvas" width="1024" height="600"></canvas>

		<div id="gameover"></div>
	</div>

	<!--<video id="inputVideo" autoplay loop></video>
	<canvas id="inputCanvas" width="200" height="150"></canvas>-->

	<div id="header" class="disable-select">
		<div id="score"></div>
		<div id="life"></div>
	</div>

	<script>

		/**
		 * Global vars
		 **/

		var canvas, // Canvas
			ctx, // 2D context
			run, // Game Loop
			WIDTH, // Game area Width
			HEIGHT, // Game area Height
			input, // Input instance
			game, // instance of Game
			tuto, // instance of Tuto
			achievement, //instance of Achievement
			currentGameObject, // instance of a GameObject
			IM, // Image Manager instance
			soundLoader, // Sound Loader instance
			isLoadedImages,
			deltaTime, //independance framerate
			TIME; 
			
		canvas = $$('#gameCanvas');
		ctx = canvas.getContext('2d');

		isLoadedImages = false;
		
		deltaTime = Date.now();
		// Uniquement si le canvas doit faire la taille de l'écran !
		/*WIDTH = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		HEIGHT = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;*/
		
		// Uniquement si le canvas a une taille fixe
		WIDTH = 1024;
		HEIGHT = 600;

		// Title Screens
		clean_bg = new Image();
		clean_bg.src = 'img/title-screen-2.jpg';
		wait_bg = new Image();
		wait_bg.src = 'img/title-screen-wait-2.jpg';
		active_bg = 'wait';


		// ========== HEADTRACKR ============
		/*var videoInput = $$('#inputVideo');
		var canvasInput = $$('#inputCanvas');

		var htracker = new headtrackr.Tracker();
		htracker.init(videoInput, canvasInput);
		htracker.start();*/
		// ========== / FIN HEADTRACKR ============

		//demarrage du jeu
		startGame = function()
		{
			run();
		}

		waitingGame = function() {
			// Waiting for press any key
			if (interval(deltaTime, 1)) {
				if (active_bg == 'clean') {
					ctx.drawImage(clean_bg,0,0);
					active_bg = 'wait';

				}
				else {
					ctx.drawImage(wait_bg,0,0);
					active_bg = 'clean';
				}
				deltaTime = +new Date();
			}
		}
		
		restartGame = function() {
			currentGameObject.hideGameover();
			currentGameObject.init();
		}

		initSounds = function()
		{
			//launch heartbeat
			var heartSlow  = soundLoader.cachedSounds[ "heart_slow2" ],
				heartFast  = soundLoader.cachedSounds[ "heart_fast2" ],
				heartShock = soundLoader.cachedSounds[ "heart_shock" ],
				heartFade  = soundLoader.cachedSounds[ "heart_fade" ],
				heartStop  = soundLoader.cachedSounds[ "heart_stop" ];
				
			var theme = soundLoader.cachedSounds[ "theme" ],
				title = soundLoader.cachedSounds[ "title" ];			
			var powerup = soundLoader.cachedSounds[ "powerup" ],
				destroy = soundLoader.cachedSounds[ "destroy" ],
				damage  = soundLoader.cachedSounds[ "damage" ],
				fire    = soundLoader.cachedSounds[ "fire" ],
				death   = soundLoader.cachedSounds[ "death" ];
				
			heartSlow.loop  = true;
			heartFast.loop  = true;
		//	heartShock.loop = true;
			
		//	damage.loop     = true;
		
		//	destroy.loop = true;
			death.loop   = true;
			
			theme.loop     = true;
			theme.volume   = 0.5;
		
			//arrête son de la mort le cas où il est activé
			death.pause();
			heartStop.pause();
			
		//	heartStop.play();
		//	fire.play();
		//	damage.play();
		//	destroy.play();
		//	powerup.play();
		//	heartSlow.play();
		//	heartFast.play();
			theme.play();
			title.pause(); //arrêt musique titre quand le jeu démarre
 		//	heartShock.play();
		//	heartFade.play();
		}
		
		/****************** GAME LOOP *******************/
		run = function(_t) {
			TIME = _t;
			if (currentGameObject.waiting === true) {
				input.listen();
				if (input.keyboard.keypressed || input.mouse.click) {
					input.mouse.click = false;
					$$('#gameArea').className += " loaded";
					currentGameObject.init();
				}
				else
					waitingGame();
			}
			else if(currentGameObject.stop === false) {
				// Sprites update
				IM.update();

				// Main game object update
				currentGameObject.update();

				// Reset single mouse click
				input.mouse.click = false;
			}
			else if(currentGameObject.cleared === true) {
				currentGameObject.clear();
			}
			requestAnimationFrame(run);
		};
		/*************** ENDING GAME LOOP ***************/
		
		// INSTANCES
		input = new Input({
            target : $$('#gameArea')
        });
		game = new Game();
		achievement = new Achievement();
		IM = new IIG.ImageManager();
	
		var path = { img : "img/", sound : "sound/" };
		soundLoader = new Loader( { w : canvas.width, h : canvas.height }, path, ctx, startGame );
	
		soundLoader.loadAssets();
	
        // Ajout des sprites du jeu qui doivent être chargés
        IM.add('img/aura.png');
        IM.add('img/shadow.png');
        IM.add('img/full_shadow.png');
        IM.add('img/logo.png');
        IM.add('img/player.png');
        IM.add('img/player_dead.png');
        IM.add('img/target.png');
        IM.add('img/ennemi_vert.png');
        IM.add('img/ennemi_marron.png');
        IM.add('img/fire_arrow.png');
        IM.add('img/explosion.png');
        IM.add('img/lightning.png');
        IM.add('img/fire_arrow_circle.png');
        IM.add('img/explosion_circle.png');
        IM.add('img/lightning_circle.png');
        IM.add('img/boss.png');
        IM.add('img/boss_hit.png');
        IM.add('img/boss_projectile.png');
        IM.add('img/blood.png');
        IM.add('img/blood2.png');
        IM.add('img/blood-player.png');
        IM.add('img/blood-player2.png');
        IM.add('img/jewel.png');
        IM.add('img/scoring-bg.png');
        IM.add('img/numbers.png');
        IM.add('img/parchemin_explosion.png');
        IM.add('img/parchemin_fire_arrow.png');
        IM.add('img/parchemin_lightning.png');
        IM.add('img/board-bg.png');
		IM.add('img/logo.png');
		IM.add('img/tuto_bg.jpg');
		IM.add('img/parchemin.png');
		IM.add('img/jewel_ico.png');

		currentGameObject = game;

		// Sprites loaded ! Lancement du jeu !
		IM.loadAll(function() {
			setTimeout(function() {
				isLoadedImages = true;
			}, 4000);
		});

		// Pour headtrackr.js
		/*var FACE = {
			x : 0,
			y : 0,
			z : 0
		}
		document.addEventListener('facetrackingEvent', function(evt) {
			FACE.x = evt.x;
			FACE.y = evt.y;
			FACE.z = evt.z;
			FACE.w = evt.width;
			FACE.h = evt.height;
		}, false);*/

	</script>

</body>
</html>